
#pragma once

#include <stdio.h>
#include "ttg/math.h"

//Generates temporary with rendom content.
class TempFile
{
	private:
		std::string filename;
	public:
		//Creates temp file.
		//Note: it will be deleted by destructor.
		TempFile(size_t file_size)
		{
			char buf[1024];
			if (GetEnvironmentVariableA("TEMP", buf, 1024) != 0)
				filename = std::string(buf) + std::string("\\TTG_TEMP_FILE.tmp");
			else
				filename = "TEMP_FILE.tmp";
			FILE *f;
			//Creating file.
			if ((f = fopen(filename.c_str(), "w")) == NULL)
			{
				filename = "";
				return;
			}
			//Filling file.
			for(size_t i = 0; i < file_size; i++)
				fputc(ttg::math::getIntRandom<char>(-127, 127), f);
			fclose(f);
		}
		//Returns name of the created file (or NULL).
		const char *getFileName()
		{ return filename.c_str(); }
		//Deletes created file.
		~TempFile()
		{
			if (!filename.empty())
				remove(filename.c_str());
		}
};
